import { ApiProperty } from '@nestjs/swagger';

import { ResponseMessage, ResponseStatus } from '../common/constants';

export class Response<T = any> {
  constructor(
    code: number,
    message: string,
    data: T = null,
    total: number = 0,
  ) {
    this.code = code;
    this.message = message;
    this.data = data;
    this.total = total;
  }

  @ApiProperty({
    description: '响应码',
    default: 0,
  })
  code: number = ResponseStatus.SUCCESS;

  @ApiProperty({
    description: '响应消息',
    default: ResponseMessage.SUCCESS,
  })
  message: string = ResponseMessage.SUCCESS;

  @ApiProperty({
    description: '响应数据',
    default: null,
  })
  data: T = null;

  @ApiProperty({
    description: '列表数据总数，非列表响应时为 0',
    default: 0,
  })
  total: number = 0;

  static success<T>(
    data: T = null,
    message = ResponseMessage.SUCCESS,
    code = ResponseStatus.SUCCESS,
  ): Response<T> {
    const response = new Response<T>(code, message, data);
    return response;
  }

  // 新增静态方法，用于返回带总数的列表数据
  static successList<T>(
    data: T[] = [],
    total: number = 0,
    message = ResponseMessage.SUCCESS,
    code = ResponseStatus.SUCCESS,
  ): Response<T[]> {
    return new Response<T[]>(code, message, data, total);
  }
  static error<T>(
    message: string = ResponseMessage.FAILURE,
    code = ResponseStatus.FAILED,
  ): Response<T> {
    const response = new Response<T>(code, message);
    return response;
  }
}
